package com.ascent.client.ui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.io.IOException;

import javax.swing.*;

import com.ascent.client.util.UserDataClient;

/**
 * 确认按钮
 */
@SuppressWarnings("serial")
public class ConfirmFrame extends JFrame {
    private JLabel tip;
    /**
     * 默认构造方法，初始化修改用户窗体
     */
    private UserDataClient userDataClient;
    public ConfirmFrame(){
        this("");
    }

    public boolean isTrue() {
        return isTrue;
    }

    boolean isTrue = false;
    /**
     * 带参数构造方法，初始化修改用户窗体
     */
    public ConfirmFrame(String title) {
        this.setTitle(title);

        Container container = this.getContentPane();
        container.setLayout(new BorderLayout());

        JPanel buttonPanel = new JPanel();

        JButton submitButton = new JButton("确认");
        JButton exitButton = new JButton("取消");

        buttonPanel.add(submitButton);
        buttonPanel.add(exitButton);

        setResizable(false);
        setSize(240, 80);
        setLocation(300, 100);

        JPanel tipPanel = new JPanel();

        tip = new JLabel();

        tipPanel.add(tip);

        container.add(BorderLayout.NORTH, tip);
        container.add(BorderLayout.SOUTH,buttonPanel);

        exitButton.addActionListener(new RejectUserActionAddListener());
        submitButton.addActionListener(new ConfirmUserActionAddListener());

        this.addWindowListener(new WindowCloser());
        this.addWindowFocusListener(new WindowFocusListener() {// 设置父窗口
            public void windowGainedFocus(WindowEvent e) {
            }
            public void windowLostFocus(WindowEvent e) {
                e.getWindow().toFront();
            }
        });
        try {
            userDataClient = new UserDataClient();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

    /**
     * 退出按钮事件监听
     * @author ascent
     */
    class RejectUserActionAddListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            isTrue = false;
            setVisible(false);
            dispose();
        }
    }

    /**
     * 确认按钮事件监听
     * @author ascent
     */
    class ConfirmUserActionAddListener implements ActionListener {
        public void actionPerformed(ActionEvent arg0) {
            isTrue = true;
            setVisible(false);
            dispose();
        }
    }
    /**
     * "关闭窗口"事件处理内部类
     * @author ascent
     */
    class WindowCloser extends WindowAdapter {
        public void windowClosing(WindowEvent e) {
            setVisible(false);
            dispose();
        }
    }
}
